package team2.game.crow;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

public class LevelBackground implements IUpdateHandler{
	
	// ===========================================================
    // Constants
    // ===========================================================
	
	// ===========================================================
    // Fields
    // ===========================================================
	private CrowActivity main;
	private backgroundSprite background;
	private boolean scrolling;
	
	// ===========================================================
    // Constructors
    // ===========================================================
	
	LevelBackground(CrowActivity main){
		this.main = main;
		background = new backgroundSprite();
		scrolling = true;
	}
	
	LevelBackground(final TextureRegion one, final TextureRegion two, CrowActivity main){
		this.main = main;
		background = new backgroundSprite(one,two);
		scrolling =true;
	}
	
	LevelBackground(final TextureRegion one, final TextureRegion two, final TextureRegion three, final TextureRegion four, final CrowActivity main){
		this.main = main;
		background = new backgroundSprite(one, two, three, four);
		scrolling = true;
	}
    // ===========================================================
    // Getter & Setter
    // ===========================================================

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================
	
	@Override
	public void onUpdate(float delta) {
		checkBounds();
	}

	@Override
	public void reset() {
		// TODO Auto-generated method stub
		
	}
	
	// ===========================================================
    // Methods
    // ===========================================================
	
	public void changeBackground(final TextureRegion one, TextureRegion two){
		background.change(one,two);
	}
	
	public void changeBackground(final TextureRegion one, final TextureRegion two, final TextureRegion three, final TextureRegion four){
		background.change(one, two, three, four);
	}
	
	public void setScrolling(boolean scroll){
		scrolling = scroll;
	}
	
	private void checkBounds(){
		if(!scrolling)
			background.deactivate();
		else
			background.activate();
			
		if(background.getY() > 720){
			background.setPosition(0);
		}
	}
	
	// ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
	
	private class backgroundSprite {
		
		Laser MasterSprite;
		
		backgroundSprite(){
			MasterSprite = null;
		}
		
		backgroundSprite(final TextureRegion one, final TextureRegion two){
			MasterSprite = new Laser(0,0,one);
			MasterSprite.attachChild(new Sprite(0,240,one));
			MasterSprite.attachChild(new Sprite(0,480,one));
			MasterSprite.attachChild(new Sprite(0,-240,one));
			MasterSprite.attachChild(new Sprite(0,-480,one));
			MasterSprite.attachChild(new Sprite(240,0,two));
			MasterSprite.attachChild(new Sprite(240,240,two));
			MasterSprite.attachChild(new Sprite(240,480,two));
			MasterSprite.attachChild(new Sprite(240,-240,two));
			MasterSprite.attachChild(new Sprite(240,-480,two));
			
			main.scene.getChild(CrowActivity.LAYER_BACKGROUND).attachChild(MasterSprite);
			
			MasterSprite.speed = 25;
			MasterSprite.direction = -1;
		}
		
		backgroundSprite(final TextureRegion one, final TextureRegion two, final TextureRegion three, final TextureRegion four){
			MasterSprite = new Laser(0,0,one);
			MasterSprite.attachChild(new Sprite(240,0,two));
			MasterSprite.attachChild(new Sprite(0,240,three));
			MasterSprite.attachChild(new Sprite(240,240,four));
			MasterSprite.attachChild(new Sprite(0,480,one));
			MasterSprite.attachChild(new Sprite(240,480,two));
			MasterSprite.attachChild(new Sprite(0,-240,three));
			MasterSprite.attachChild(new Sprite(240,-240,four));
			MasterSprite.attachChild(new Sprite(0,-480,one));
			MasterSprite.attachChild(new Sprite(240,-480,two));
			
			main.scene.getChild(CrowActivity.LAYER_BACKGROUND).attachChild(MasterSprite);
			
			MasterSprite.speed = 25;
			MasterSprite.direction = -1;
		}
		
		public float getY(){
			return (MasterSprite.getY()+240);
		}
		
		public void setPosition(float y){
			MasterSprite.setPosition(MasterSprite.getX(), y);
		}
		
		public void deactivate(){
			MasterSprite.active = false;
		}
		
		public void activate(){
			MasterSprite.active = true;
		}
		
		public void change(final TextureRegion one, final TextureRegion two){
			main.runOnUpdateThread(new Runnable() {
	            @Override
	            public void run() {
	            	if(MasterSprite != null){
	            		MasterSprite.detachChildren();
	            		main.scene.getChild(CrowActivity.LAYER_BACKGROUND).detachChild(MasterSprite);
	            	}
	            	MasterSprite = new Laser(0,0,one);
	    			MasterSprite.attachChild(new Sprite(0,240,one));
	    			MasterSprite.attachChild(new Sprite(0,480,one));
	    			MasterSprite.attachChild(new Sprite(0,-240,one));
	    			MasterSprite.attachChild(new Sprite(0,-480,one));
	    			MasterSprite.attachChild(new Sprite(240,0,two));
	    			MasterSprite.attachChild(new Sprite(240,240,two));
	    			MasterSprite.attachChild(new Sprite(240,480,two));
	    			MasterSprite.attachChild(new Sprite(240,-240,two));
	    			MasterSprite.attachChild(new Sprite(240,-480,two));
	    			
	    			main.scene.getChild(CrowActivity.LAYER_BACKGROUND).attachChild(MasterSprite);
	    			
	    			MasterSprite.speed = 25;
	    			MasterSprite.direction = -1;
	            }
			});
		}
		
		public void change(final TextureRegion one, final TextureRegion two, final TextureRegion three, final TextureRegion four){
			main.runOnUpdateThread(new Runnable() {
	            @Override
	            public void run() {
	            	if(MasterSprite != null){
	            		MasterSprite.detachChildren();
	            		main.scene.getChild(CrowActivity.LAYER_BACKGROUND).detachChild(MasterSprite);
	            	}
	            	MasterSprite = new Laser(0,0,one);
	    			MasterSprite.attachChild(new Sprite(240,0,two));
	    			MasterSprite.attachChild(new Sprite(0,240,three));
	    			MasterSprite.attachChild(new Sprite(240,240,four));
	    			MasterSprite.attachChild(new Sprite(0,480,one));
	    			MasterSprite.attachChild(new Sprite(240,480,two));
	    			MasterSprite.attachChild(new Sprite(0,-240,three));
	    			MasterSprite.attachChild(new Sprite(240,-240,four));
	    			MasterSprite.attachChild(new Sprite(0,-480,one));
	    			MasterSprite.attachChild(new Sprite(240,-480,two));
	    			
	    			main.scene.getChild(CrowActivity.LAYER_BACKGROUND).attachChild(MasterSprite);
	    			
	    			MasterSprite.speed = 25;
	    			MasterSprite.direction = -1;
	            }
			});
		}
		
	}

}
